//
//  WFStatusOriginalView.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/13.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WFStatusOriginalView: UIView {
    //记录原创微博底部的约束
    var originalViewBottom:Constraint?
    
    // MARK:  - 持有对应显示的statusviewmodel
    var statusViewModel:WFStatusViewModel? {
        //设置属性观察器
        didSet{
            if let imgageUrl = statusViewModel?.status?.user?.profile_image_url {
                headImageView.sd_setImageWithURL(NSURL(string: imgageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            mbrankImageView.image = statusViewModel?.mbrandImage
            
            // MARK:  - 时间的处理还没有进行
            timeLabel.text = statusViewModel?.timeStr
            sourceLabel.text = statusViewModel?.sourceContent
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            
            //卸载上一次的约束
            originalViewBottom?.uninstall()
            //判断是否有转发微博配图
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
                //说明是有配图的
                //那就设置模型
                
                pictureView.picUrls = statusViewModel?.status?.pic_urls
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                    
                })
                
            }else{
                //说明没有配图
                 pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
               
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK:  - 懒加载控件
    //头像
    private lazy var headImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel:UILabel = UILabel(textColor: UIColor.grayColor(), fontSize: HomeFontSize)
    //会员等级
    private lazy var mbrankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    //微博内容
    private lazy var contentLabel:UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HomeFontSize)
        label.numberOfLines = 0
        return label
    }()
    
    //原创微博配图
    private lazy var pictureView:WFStatusPictureView = {
        let collectionView = WFStatusPictureView()
        
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()

    
   // MARK:  -  设置视图
    private func setUpUI(){
        backgroundColor = UIColor.whiteColor()
        //添加子控件
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //手动添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
          make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
          make.top.equalTo(headImageView)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(headImageView)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        //原创配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        //原创微博一个很重要的约束
        self.snp_makeConstraints { (make) -> Void in
          self.originalViewBottom =   make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
    

}
